package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@Tag(name = "公寓信息管理")
@RestController
@RequestMapping("/admin/apartment")
public class ApartmentController {

    @Autowired
    private ApartmentInfoService apartmentInfoService ;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService ;

    @Autowired
    private ApartmentLabelService apartmentLabelService ;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService ;

    @Autowired
    private GraphInfoService graphInfoService ;

    @Autowired
    private RoomInfoService roomInfoService ;

    @Operation(summary = "保存或更新公寓信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ApartmentSubmitVo apartmentSubmitVo) {

        Long apartmentSubmitVoId = apartmentSubmitVo.getId();

        if(apartmentSubmitVoId!=null){
            //1.删除旧的配套关系
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,apartmentSubmitVoId));
            //2.删除旧的标签关系
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,apartmentSubmitVoId));
            //3.删除旧的杂费值关系
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVoId));
            //4.删除旧的图片列表
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                    .eq(GraphInfo::getItemType,ItemType.APARTMENT)
                    .eq(GraphInfo::getItemId,apartmentSubmitVoId)
            );
        }

        //1.保存公寓基本信息
        apartmentInfoService.saveOrUpdate(apartmentSubmitVo);

        //2.保存公寓配套信息
        /*
        apartmentSubmitVo.getFacilityInfoIds().forEach(facilityId->{
            ApartmentFacility apartmentFacility = ApartmentFacility.builder().facilityId(facilityId).apartmentId(apartmentSubmitVo.getId()).build();
            apartmentFacilityService.saveOrUpdate(apartmentFacility);
        });
         */
        apartmentFacilityService.saveOrUpdateBatch(apartmentSubmitVo.getFacilityInfoIds().stream().map(facilityId->{
            return ApartmentFacility.builder().facilityId(facilityId).apartmentId(apartmentSubmitVo.getId()).build() ;
        }).collect(Collectors.toList()));

        //3.保存公寓标签信息
        apartmentSubmitVo.getLabelIds().forEach(labelId->{
            ApartmentLabel apartmentLabel = ApartmentLabel.builder().labelId(labelId).apartmentId(apartmentSubmitVo.getId()).build();
            apartmentLabelService.saveOrUpdate(apartmentLabel);
        });

        //4.保存公寓杂费值信息
        apartmentSubmitVo.getFeeValueIds().forEach(feeValueId->{
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().feeValueId(feeValueId).apartmentId(apartmentSubmitVo.getId()).build();
            apartmentFeeValueService.saveOrUpdate(apartmentFeeValue);
        });


        //5.保存图片信息
        apartmentSubmitVo.getGraphVoList().forEach(vo->{
            GraphInfo graphInfo = new GraphInfo();
            BeanUtils.copyProperties(vo,graphInfo);
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfo.setItemId(apartmentSubmitVo.getId());
            graphInfoService.saveOrUpdate(graphInfo);
        });

        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询公寓列表")
    @GetMapping("pageItem")
    public Result<IPage<ApartmentItemVo>> pageItem(@RequestParam long current, @RequestParam long size, ApartmentQueryVo queryVo) {
        Page page = Page.of(current,size);
        IPage apartmentItemPage = apartmentInfoService.pageApartmentItemByQuery(page, queryVo);
        return Result.ok(apartmentItemPage);
    }

    @Operation(summary = "根据ID获取公寓详细信息")
    @GetMapping("getDetailById")
    public Result<ApartmentDetailVo> getDetailById(@RequestParam Long id) {
        return Result.ok(apartmentInfoService.getDetailById(id));
    }

    @Operation(summary = "根据id删除公寓信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {

        //1.查询指定公寓包含的房间数。如果有房间，则不可以删除公寓信息
        if(roomInfoService.getRoomCountByApartmentId(id)>0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        //2-1 删除公寓本身的信息
        apartmentInfoService.removeById(id);
        //2-2 删除图片信息
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType,ItemType.APARTMENT).eq(GraphInfo::getItemId,id));
        //2-3 删除标签关系
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
        //2-4 删除配套关系
        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
        //2-5 删除杂费值
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));

        return Result.ok();
    }

    @Operation(summary = "根据id修改公寓发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id, @RequestParam ReleaseStatus status) {
        apartmentInfoService.update(new LambdaUpdateWrapper<ApartmentInfo>().set(ApartmentInfo::getIsRelease,status).eq(ApartmentInfo::getId,id));
        return Result.ok();
    }

    @Operation(summary = "根据区县id查询公寓信息列表")
    @GetMapping("listInfoByDistrictId")
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam Long id) {
        return Result.ok(apartmentInfoService.list(new LambdaQueryWrapper<ApartmentInfo>().eq(ApartmentInfo::getDistrictId,id)));
    }
}














